package com.springBoot.filter;

import com.springBoot.commonClass.JwtUtils;
import com.springBoot.commonClass.PassToken;
import com.springBoot.exception.BusinessFailureException;
import com.springBoot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.Map;

public class JwtAuthenticationInterceptor implements HandlerInterceptor {
    @Autowired
    UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        //从请求头中取出 token 这里需要和前端约定好把jwt放到请求头一个叫token的地方
        String token = httpServletRequest.getHeader("token");
        //如果不是映射到方法直接通过
        if(!(object instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) object;
        Method method = handlerMethod.getMethod();
        //检查是否有passtoken注释，有则跳过认证
        if (method.isAnnotationPresent(PassToken.class)){
            PassToken passToken = method.getAnnotation(PassToken.class);
            if(passToken.required()){
                return true;
            }
        }
        //默认全部检查
        else{
            System.out.println("被jwt拦截需要验证");
            //执行认证
            if (token == null){
                //登录失效，没token了
//                throw new Exception("登陆失效");
                throw new BusinessFailureException(true,new Exception("token信息失效"));
            }

            //获取token中的userName
            String userId = JwtUtils.getAudience(token);

            //找找看是否有这个user， 验证用户是否存在
            Map<String, String> res = userService.getUserByUserId(userId);
            if(res == null){
                throw new Exception("用户不存在");
//                throw new BusinessFailureException(true);
            }
            //验证 token
            JwtUtils.verifyToken(token, userId);
            //获取载荷内容
            String userName = JwtUtils.getClaimByName(token, "userName").asString();
            String userAcc = JwtUtils.getClaimByName(token, "userAcc").asString();

            //放入attribute以便后面调用
            httpServletRequest.setAttribute("userName", userName);
            httpServletRequest.setAttribute("userAcc", userAcc);
            return true;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest,
                           HttpServletResponse httpServletResponse,
                           Object o, ModelAndView modelAndView){

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest,
                                HttpServletResponse httpServletResponse,
                                Object o, Exception e){}

}
